package com.ruoyi.banner.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.banner.domain.vo.BannerVo;
import com.ruoyi.banner.domain.bo.BannerBo;
import com.ruoyi.banner.service.IBannerService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * Banner管理
 *
 * @author ruoyi
 * @date 2022-08-18
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/banner/banner")
public class BannerController extends BaseController {

    private final IBannerService iBannerService;

    /**
     * 查询Banner管理列表
     */
    @SaCheckPermission("banner:banner:list")
    @GetMapping("/list")
    public TableDataInfo<BannerVo> list(BannerBo bo, PageQuery pageQuery) {
        return iBannerService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出Banner管理列表
     */
    @SaCheckPermission("banner:banner:export")
    @Log(title = "Banner管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BannerBo bo, HttpServletResponse response) {
        List<BannerVo> list = iBannerService.queryList(bo);
        ExcelUtil.exportExcel(list, "Banner管理", BannerVo.class, response);
    }

    /**
     * 获取Banner管理详细信息
     *
     * @param bannerId 主键
     */
    @SaCheckPermission("banner:banner:query")
    @GetMapping("/{bannerId}")
    public R<BannerVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long bannerId) {
        return R.ok(iBannerService.queryById(bannerId));
    }

    /**
     * 新增Banner管理
     */
    @SaCheckPermission("banner:banner:add")
    @Log(title = "Banner管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BannerBo bo) {
        return toAjax(iBannerService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改Banner管理
     */
    @SaCheckPermission("banner:banner:edit")
    @Log(title = "Banner管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BannerBo bo) {
        return toAjax(iBannerService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除Banner管理
     *
     * @param bannerIds 主键串
     */
    @SaCheckPermission("banner:banner:remove")
    @Log(title = "Banner管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{bannerIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] bannerIds) {
        return toAjax(iBannerService.deleteWithValidByIds(Arrays.asList(bannerIds), true) ? 1 : 0);
    }
}
